//
//  BodyProfileViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/8/30.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "BodyProfileViewController.h"
#import "CaseHistoryViewController.h"
#import "PopulationViewController.h"
#import "SelectView.h"
#import "SelectYearsView.h"

@interface BodyProfileViewController ()<CaseHistoryViewControllerDelegate,PopulationViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *labHeight;
@property (weak, nonatomic) IBOutlet UILabel *labBodyWeight;
@property (weak, nonatomic) IBOutlet UILabel *labAge;
@property (weak, nonatomic) IBOutlet UIButton *btnGenderMale;//男
@property (weak, nonatomic) IBOutlet UIButton *btnGenderFemale;//女
@property (weak, nonatomic) IBOutlet UILabel *labPopulation;
@property (strong, nonatomic) NSMutableArray *medicalHistory;
@property (assign, nonatomic) BOOL gender;
@property (strong, nonatomic) NSString *age;
@property (strong, nonatomic) NSString *weight;
@property (strong, nonatomic) NSString *height;
@end

@implementation BodyProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"身体资料";
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    [self showNavBarWithRightImage:[UIImage imageNamed:@"icon_save"] andRightBtnAction:@selector(save) LeftBtnImage:nil andLeftBtnAction:nil title:nil barColor:nil];
    NSDictionary *dict = [self.response objectForKey:@"result"];
    self.age = [dict objectForKey:@"birth_year"];
    if (self.age != nil && self.age != NULL && ![self.age isEqualToString:@""] ) {
        self.labAge.text  = [NSString stringWithFormat:@"%@ 年",self.age];
    }else{
        self.labAge.text = @"";
    }
    self.height = [dict objectForKey:@"height"];
    if (self.height != nil && self.height != NULL && ![self.height isEqualToString:@""] ) {
        self.labHeight.text = [NSString stringWithFormat:@"%@ cm",self.height];
    }else{
        self.labHeight.text = @"";
    }
    self.weight = [dict objectForKey:@"weight"];
    if (self.weight != nil && self.weight != NULL && ![self.weight isEqualToString:@""] ) {
        self.labBodyWeight.text = [NSString stringWithFormat:@"%@ kg",self.weight];
    }else{
        self.labBodyWeight.text = @"";
    }
    self.medicalHistory = [NSMutableArray array];
    self.medicalHistory = [dict objectForKey:@"medical_history"];
    if ([[dict objectForKey:@"gender"] intValue] == 1) {
        self.gender = YES;//男
        [self.btnGenderFemale setImage:[UIImage imageNamed:@"radio"] forState:UIControlStateNormal];
    }else{
        self.gender = NO;
        [self.btnGenderMale setImage:[UIImage imageNamed:@"radio"] forState:UIControlStateNormal];
    }
    self.labPopulation.text = [dict objectForKey:@"crowdtype"];
    self.tableView.scrollEnabled = NO;
}

#pragma mark - 数据请求
- (void)modifyContacts{
    [SVProgressHUD showWithStatus:@"修改身体资料..." maskType:SVProgressHUDMaskTypeClear];
    int gen;
    if (self.gender) {
        gen = 1;
    }else{
        gen = 0;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    [dict setObject:[NSNumber numberWithInt:gen] forKey:@"new_gender"];
    [dict setObject:self.height forKey:@"new_height"];
    [dict setObject:self.weight forKey:@"new_weight"];
    [dict setObject:self.age forKey:@"new_birth_year"];
    [dict setObject:self.medicalHistory forKey:@"new_medical_history"];
    [dict setObject:self.labPopulation.text forKey:@"new_crowdtype"];
    [[BaseDataRequest alloc] modifyBodyProfole:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                NSString *message = [responseObject objectForKey:@"message"];
                if ([message isEqualToString:@""]) {
                    TIPSFORVIEW(@"修改身体失败");
                }else{
                    TIPSFORVIEW(message);
                }
            }
        }
    }];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 5){
        //病史
        CaseHistoryViewController *caseHistory = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"CaseHistoryViewController"];
        caseHistory.response = self.response;
        caseHistory.deleaget = self;
        [self.navigationController pushViewController:caseHistory animated:YES];
    }else if (indexPath.row == 3){
        //出生年份
        SelectYearsView *select = [[SelectYearsView alloc] init:^(NSString *str) {
            self.age = str;
            _labAge.text = [NSString stringWithFormat:@"%@ 年",str];
        }];
        select.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self.view addSubview:select];
    }else if (indexPath.row == 2){
        //体重
        SelectView *select = [[SelectView alloc] init:^(NSString *str) {
            self.weight = str;
            self.labBodyWeight.text = [NSString stringWithFormat:@"%@ kg",str];
        }];
        select.selectType = SelectTypeBodyWeight;
        select.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self.view addSubview:select];
    }else if(indexPath.row == 1){
        //身高
        SelectView *select = [[SelectView alloc] init:^(NSString *str) {
            self.height = str;
            self.labHeight.text = [NSString stringWithFormat:@"%@ cm",str];
        }];
        select.selectType = SelectTypeHeight;
        select.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self.view addSubview:select];
    }else if(indexPath.row == 4){
        //人群分类
        PopulationViewController *population = [[UIStoryboard storyboardWithName:@"PersonalViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"PopulationViewController"];
        population.crowdtype = self.labPopulation.text;
        population.deleaget = self;
        [self.navigationController pushViewController:population animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - CaseHistoryViewControllerDelegate
- (void)backSelect:(NSMutableArray *)arr{
    self.medicalHistory = arr;
    NSMutableDictionary *dict = [self.response objectForKey:@"result"];
    [dict setObject:arr forKey:@"medical_history"];
    [self.response setObject:dict forKey:@"result"];
}

#pragma mark - PopulationViewControllerDelegate
- (void)backSelectClass:(NSString *)strName{
    self.labPopulation.text = strName;
}

#pragma mark -
- (void)save{
    [self modifyContacts];
}


- (IBAction)selectFemale:(id)sender {
    self.gender = NO;
    [self.btnGenderFemale setImage:[UIImage imageNamed:@"icon_usergender"] forState:UIControlStateNormal];
    [self.btnGenderMale setImage:[UIImage imageNamed:@"radio"] forState:UIControlStateNormal];
}

- (IBAction)selectMale:(id)sender {
    self.gender = YES;
    [self.btnGenderFemale setImage:[UIImage imageNamed:@"radio"] forState:UIControlStateNormal];
    [self.btnGenderMale setImage:[UIImage imageNamed:@"icon_usergender"] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
